package com.suray.wcs.service.system.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suray.commin.core.utils.SecurityUtils;
import com.suray.commin.core.utils.StringUtils;
import com.suray.wcs.service.system.entity.*;
import com.suray.wcs.service.system.mapper.RgvTestDBMapper;
import com.suray.wcs.service.system.service.IRgvTestDBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class RgvTestDBServiceImpl extends ServiceImpl<RgvTestDBMapper, RgvTestDB> implements IRgvTestDBService {
    @Autowired
    RgvTestDBMapper rgvTestDBMapper;

    /**
     * 条件查询小车信息
     *
     * @return
     */
    @Override
    public List<RgvTestDB> getRgvTestList(RgvTestDB rgvTestDB) {
        QueryWrapper<RgvTestDB> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(rgvTestDB.getRgvSerialNumber())) {
            queryWrapper.lambda().like(RgvTestDB::getRgvSerialNumber, rgvTestDB.getRgvSerialNumber());
        }
        List<RgvTestDB> RgvTestDBS = rgvTestDBMapper.selectList(queryWrapper);
        // 返回结果
        return RgvTestDBS;
    }

    /**
     * 小车测试Id获取DB对象
     *
     * @param rgvId
     * @return
     */
    @Override
    public RgvTestDB getRgvTestDBByNo(Integer rgvId) {
        QueryWrapper<RgvTestDB> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(RgvTestDB::getRgvId, rgvId);
        return getOne(queryWrapper);
    }

    /**
     * 根据小车序列号查询任务
     *
     * @param rgvSerialNo
     * @return
     */
    @Override
    public RgvTestDB queryRgvInfoBySerialNo(String rgvSerialNo) {
        List<RgvTestDB> rgvTestDBS = this.list(new QueryWrapper<RgvTestDB>().lambda().eq(RgvTestDB::getRgvSerialNumber, rgvSerialNo));
        if (rgvTestDBS == null || rgvTestDBS.size() == 0) {
            return null;
        }
        return rgvTestDBS.get(0);
    }
    @Override
    public boolean checkSerialNoUnique(RgvTestDB rgvTestDB) {
        Long rgvId = StringUtils.isNull(rgvTestDB.getRgvId()) ? -1L : rgvTestDB.getRgvId();
        RgvTestDB info = this.queryRgvInfoBySerialNo(rgvTestDB.getRgvSerialNumber());
        if (StringUtils.isNotNull(info) && info.getRgvId().longValue() != rgvId.longValue()) {
            return true;
        }
        return false;
    }
    @Override
    public boolean addRgvTest(RgvTestDB rgvTestDB) {
        rgvTestDB.setCreateBy(SecurityUtils.getUsername());
        rgvTestDB.setCreateTime(new Date());
        boolean result = this.save(rgvTestDB);
        return result;
    }
    @Override
    public boolean updateRgvTest(RgvTestDB rgvTestDB) {
        boolean success = this.updateById(rgvTestDB);
        return success;
    }
}
